\CUSProvideExplLibrary{ref}{\cus@d@te}{\cus@versi@n}{cross reference}

\int_new:N \g__cus_ref_record_page_int 
\cs_gset_protected:Npn \IfPageOdd { \__cus_ref_if_page_odd:N \c@page }
\cs_gset_protected:Npn \IfAbsPageOdd 
  { \__cus_ref_if_page_odd:N \g_shipout_readonly_int }
\cs_gset_protected:Npn \__cus_ref_if_page_odd:N #1
  {
    \int_gincr:N \g__cus_ref_record_page_int
    \cus_newlabel_shipout_x:xxxxxx 
      { cus@ref/pageodd/cnt/ \int_use:N \g__cus_ref_record_page_int }
      { \int_use:N \g__cus_ref_record_page_int } 
      { \exp_not:N \int_value:w #1 } { } { } 
      { \exp_not:N \int_value:w \g_shipout_readonly_int }
    \cus_label_if_exist:nTF 
      { cus@ref/pageodd/cnt/ \int_use:N \g__cus_ref_record_page_int }
      {
        \int_if_odd:nTF { \int_value:w \__cus_get_label_page:n { cus@ref/pageodd/cnt/ \int_use:N \g__cus_ref_record_page_int } }
          { \use_i:nn } { \use_ii:nn }
      }
      { 
        \G@refundefinedtrue 
        \if_int_odd:w #1 \exp_after:wN \use_i:nn \else: \exp_after:wN \use_ii:nn \fi:
      }
  }

\NewDocumentCommand \HyperRef { s m O{} }
  {
    \tl_if_blank:nTF {#3} 
      { \IfBooleanTF {#1} \cus_ref_label_shbox:nn \cus_ref_label_box:nn {#2} }
      { \exp_last_unbraced:Ne \HyperBoxRef { \IfBooleanT {#1} {*} } {#2} {#3} }
  }
\NewDocumentCommand \HyperBoxRef { s m m O{c} O{\relax} O{s} }
  {
    \IfBooleanTF{#1} \collectn_varwidth:Nnnnnnw \collectn_width:Nnnnnnw 
      \l__cus_tmpc_box
      { \cus_ref_label:nn {#2} { \XeTeXLinkBox { \box_use_drop:N \l__cus_tmpc_box } } }
      {#4} {#5} {#6} {#3}
  }
\NewDocumentCommand \HyperLink { s m O{} }
  {
    \tl_if_blank:nTF {#3} 
      { \IfBooleanTF {#1} \cus_ref_target_shbox:nn \cus_ref_target_box:nn {#2} }
      { \exp_last_unbraced:Ne \HyperBoxLink { \IfBooleanT {#1} {*} } {#2} {#3} }
  }
\NewDocumentCommand \HyperBoxLink { s m m O{c} O{\relax} O{s} }
  {
    \IfBooleanTF{#1} \collectn_varwidth:Nnnnnnw \collectn_width:Nnnnnnw 
      \l__cus_tmpc_box
      { \cus_ref_target:nn {#2} { \XeTeXLinkBox { \box_use_drop:N \l__cus_tmpc_box } } }
      {#4} {#5} {#6} {#3}
  }